package gui;

// Used for creating the realtime graphical view
import graphs.RealtimeGraph;

// Java specific imports
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

// Importing the mechanics
import mechanics.*;

// Swing specific imports
import org.dyno.visual.swing.layouts.Bilateral;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

/**
 * MainGUI creates the GUI of the program and updates the sensor values
 * 
 * @version 31-03-2009
 */
public class MainGUI extends JFrame {

	// Serial type
	private static final long serialVersionUID = 1L;

	// The main panel, which holds the tabs
	private JTabbedPane panel_Main;

	// The tab for the Huidige Staat
	private HuidigeStaat huidigestaat;
	// The tab for the Geschiedenis
	private Geschiedenis geschiedenis;
	// The tab for the Instellinge
	private Instellingen instellingen;
	// The panel for the graphical representation
	private JPanel panel_GrafischeWeergave;

	// The realtime graphical generator
	private RealtimeGraph rtGraph;
	// The controller for reading out the sensors
	private Controller controller;
	// The timer used for updating
	private java.util.Timer timer;

	// The current measurement
	private Meting meting;

	// The delay between updates
	private static final int delay = 5000;

	/**
	 * Constructor loads the controller, builds the gui and starts refreshing
	 * 
	 * @param controller The controller used for communicating with the hardware
	 */
	public MainGUI(Controller controller) {
		// Load the controller
		this.controller = controller;

		// Create the Geschiedenis tab
		geschiedenis = new Geschiedenis();
		// Create the HuidigeStaat tab
		huidigestaat = new HuidigeStaat();
		// Create the Instellingen tab
		instellingen = new Instellingen(controller);
		// Set the timer
		timer = new java.util.Timer();
		// Load the realtime graphical view
		rtGraph = new RealtimeGraph();

		// Load the components of the GUI
		initComponents();

		// Start the timer
		timer.schedule(new java.util.TimerTask() {
			public void run() {
				updateMeting();
			}
		}, 0, delay);

		// Create the actual window
		setDefaultCloseOperation(MainGUI.EXIT_ON_CLOSE);
		setTitle("Project Kas");
		getContentPane().setPreferredSize(getSize());
		pack();
		setResizable(false);
		setLocationRelativeTo(null);
		setVisible(true);
	}

	// Refresh the onscreen values
	public void refresh() {
		// Update the sensor values
		huidigestaat.setText(meting.getHumidity() + "", meting.getLight() + "", meting.getTemperature() + "");

		// Update the realtime graphical view
		this.rtGraph.addMeting(meting);
		panel_Main.setComponentAt(3, rtGraph.getGraph());
	}

	/**
	 * Update the measurements
	 */
	public void updateMeting() {
		// Load in a new measurement
		meting = new Meting(getDate(), getTime(), controller.getHumidity(), controller.getLight(), controller
				.getTemperature());

		// Refresh the GUI in a new thread
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				refresh();
			}
		});
	}

	/**
	 * Returns the date of today in a human format
	 * 
	 * @return The date
	 */
	private String getDate() {
		Calendar now = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		return formatter.format(now.getTime());
	}

	/**
	 * Returns the time of now in a human format
	 * 
	 * @return The time
	 */
	private String getTime() {
		Calendar now = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");
		return formatter.format(now.getTime());
	}

	/**
	 * Loads the components in the GUI
	 */
	private void initComponents() {
		setTitle("TestFrame");
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setFont(new Font("Dialog", Font.PLAIN, 12));
		setState(Frame.ICONIFIED);
		setExtendedState(Frame.ICONIFIED);
		setForeground(Color.black);
		setLayout(new GroupLayout());
		add(getPanel_Main(), new Constraints(new Bilateral(0, 0, 5), new Leading(0, 404, 10, 10)));
		setSize(707, 406);
	}

	/**
	 * Returns the panel used for the realtime graphical view
	 * 
	 * @return The realtime graphical panel
	 */
	private JPanel getPanel_GrafischeWeergave() {
		if (panel_GrafischeWeergave == null) {
			panel_GrafischeWeergave = new JPanel();
			panel_GrafischeWeergave.setVisible(false);
			panel_GrafischeWeergave.setLayout(new GroupLayout());
		}
		return panel_GrafischeWeergave;
	}

	/**
	 * Creates the panel holding the different tabs
	 * 
	 * @return The main panel
	 */
	private JTabbedPane getPanel_Main() {
		if (panel_Main == null) {
			panel_Main = new JTabbedPane();
			panel_Main.addTab("Huidige staat", huidigestaat.getPanel());
			panel_Main.addTab("Geschiedenis", geschiedenis.getPanel());
			panel_Main.addTab("Instellingen", instellingen.getPanel());
			panel_Main.addTab("Grafische weergave", getPanel_GrafischeWeergave());
		}
		return panel_Main;
	}
}
